package P6.T04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class JMyFrame extends JFrame {
    public JMyFrame() {
        this.setTitle("My Frame");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        setJMenuBar(buildMenu());
        JPopupMenu popMenu=buildPopMenu();
        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if(e.getButton()==3){
                    popMenu.show(JMyFrame.this,e.getX(),e.getY());

                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        this.setVisible(true);

    }

    private JPopupMenu buildPopMenu() {
        JPopupMenu main=new JPopupMenu();
        JMenuItem oneItem=new JMenuItem("one");
        JMenuItem twoItem=new JMenuItem("two");
        JMenuItem threeItem=new JMenuItem("three");
        main.add(oneItem);
        main.add(twoItem);
        main.add(threeItem);
        return main;
    }

    private JMenuBar buildMenu() {
        JMenuBar bar=new JMenuBar();
        JMenu fileMenu=new JMenu("文件");
        JMenu editMenu=new JMenu("编辑");
        JMenu viewMenu=new JMenu("视图");
        JMenu helpMenu=new JMenu("帮助");
        JMenu newFileMenu=new JMenu("新建");
        JMenuItem docMenuItem=new JMenuItem("文档");
        JMenuItem printMenuItem=new JMenuItem("打印");
        bar.add(fileMenu);
        bar.add(editMenu);
        bar.add(viewMenu);
        bar.add(helpMenu);
        fileMenu.add(newFileMenu);
        newFileMenu.add(docMenuItem);
        fileMenu.add(printMenuItem);
        Icon icon=new ImageIcon("src/P6/T04/001.png");
        fileMenu.setIcon(icon);


        printMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"打印啦");
            }
        });
        return bar;
    }
}
